<?php
 require_once('menu.php');
 if ($offerId) {
    $url = "/{$offerId}";
 } else {
    $url = "";
 }

 echo $javascript->link('tiny_mce/tiny_mce.js');
 ?>
<script type="text/javascript">
    $().ready(function() {
        $("#businessForm").validate({
            errorLabelContainer: $("#businessForm div.error")
        });
    });

    tinyMCE.init({
    	// General options
    	mode : "textareas",
    	theme : "advanced",
    	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",

    	// Theme options
    	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
    	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code",
    	theme_advanced_buttons3 : "insertdate,inserttime,preview,|,forecolor,backcolor",
    	theme_advanced_toolbar_location : "top",
    	theme_advanced_toolbar_align : "left",
    	theme_advanced_statusbar_location : "bottom",
    	theme_advanced_resizing : true,

    	// Example content CSS (should be your site CSS)
    	content_css : "css/example.css",

    	// Drop lists for link/image/media/template dialogs
    	template_external_list_url : "js/template_list.js",
    	external_link_list_url : "js/link_list.js",
    	external_image_list_url : "js/image_list.js",
    	media_external_list_url : "js/media_list.js",

    	// Replace values for the template plugin
    	template_replace_values : {
    		username : "Some User",
    		staffid : "991234"
    	}
    });

    function getsysdatetime(elmnt, format)
    {
        var mydate = new Date();
        if (format == 1) {
        	var timestamp = mydate.getFullYear() + "-" + (mydate.getMonth() + 1) + "-" + mydate.getDate() + " " + mydate.getHours() + ":" + mydate.getMinutes() + ":" + mydate.getSeconds();
		} else {
			var timestamp = (mydate.getMonth() + 1) + "/" + mydate.getDate() + "/" + mydate.getFullYear() + " " + mydate.getHours() + ":" + mydate.getMinutes() + ":" + mydate.getSeconds();
		}

        document.getElementById(elmnt).value = timestamp;
    }

    function yhostip()
    {
        if ((navigator.appName == "Microsoft Internet Explorer") &&  ((navigator.appVersion.indexOf('3.') != -1) || (navigator.appVersion.indexOf('4.') != -1))) {
            document.write("Not with MS IE 3.0/4.0");
        } else {
            window.onerror=null;
            yourAddress=java.net.InetAddress.getLocalHost();
            yourAddress2=java.net.InetAddress.getLocalHost();
            yhost=yourAddress.getHostName();
            yip=yourAddress2.getHostAddress();
            document.getElementById('ip_val').value = yip;
            //document.write("Your host name is "+yhost);
            //document.write("<br>Your IP address is "+yip);
		}
	}
</script>

<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="menudottedline" width="40%">
        <?php
         if (isset($alert)) {
         ?>
            <div class="pathway">Alert:&nbsp;<?=$alert?></div>
        <?php
         }
         ?>
        </td>
        <td class="menudottedline" align="right">
            <table cellpadding="0" cellspacing="0" border="0" id="toolbar">
                <tr height="60" valign="middle" align="center">
                    <td>
                        <a class="toolbar" href="javascript: document.getElementById('flag').value='offer'; submitthis();">
                        <?php echo $html->image('admin/mediamanager_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Offers
                        </a>
                    </td>
                    <td>
                        <a class="toolbar" href="javascript: document.getElementById('flag').value='cancel'; submitthis();">
                        <?php echo $html->image('admin/cancel_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Cancel
                        </a>
                    </td>
                    <td>
                        <a class="toolbar" href="#">
                        <?php echo $html->image('admin/help_f2.png', array('align' => 'middle', 'border' => 0)); ?><br>Help
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br>
<br>
<div align="center" class="centermain">
    <div class="main">
        <table class="adminheading">
            <tr>
                <th class="info" rowspan="2" nowrap><?php echo $page_title; ?></th>
            </tr>
        </table>


        <table class="adminform">
            <tr>
                <td width="60%" valign="top">
                    <form id="businessForm" name="businessForm" method="post" action="<?=$html->url('/admin/add_offers'.$url)?>" enctype="multipart/form-data">
                    <div class="error"></div>
                    <table class="adminlist" cellpadding=20>
                    	<tr>
                    		<th>Offer Details</th>
                    	</tr>
                    	<tr>
                    		<td align="left"></td>
                    	</tr>
                    </table>

                    <table width="100%">
                        <tr>
                            <td width="60%">
                                <table class="adminlist" cellpadding=20>
                                    <tr>
                                        <td align="left" width="20%"><font color="red"><b>*</b></font>&nbsp;Networks</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                $network_val = $offers['Offer']['ad_network'];
                                             } else {
                                                $network_val = "";
                                             }
                                             echo $form->select('ad_network', $network, $network_val, array('id' => 'network', 'title' => 'Network Ad is a required field', 'class' => '{required:true}', 'style' => 'width: 250px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left"><font color="red"><b>*</b></font>&nbsp;Offer name</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][offer_name]" id="offer_name" style="width: 400px" value="<?php echo ($offers) ? $offers['Offer']['offer_name'] : ""; ?>" title="Offer name is a required field<br/>" class="{required:true}">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Offer Link-out URL</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][offer_url]" id="field_length" title="" class="" style="width: 400px" value="<?php echo ($offers) ? $offers['Offer']['offer_url'] : ""; ?>">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left" valign="top">&nbsp;Offer Creative 1</td>
                                        <td align="left" style='border: 1px dotted #ccc; background-color: #efefef;'>
                                            <input type="file" id="creative1" name="data[offer][creative_1]" value="<?php echo ($offers) ? $offers['Offer']['creative_1'] : ""; ?>">
                                            <label style="color: red; font-size: 9px;">Required image format is jpg</label>
                                            <?php
                                             echo "&nbsp;&nbsp;Image Size:&nbsp;" . $form->select('offer.creative1_size', $image_sizes, null, array('id' => 'creative1_size', 'style' => 'width: 150px'));
                                             if ($offers) {
                                                if ($offers['Offer']['creative_1']) {
                                             ?>
                                            <a href="<?php echo $html->url('/admin/preview/'.base64_encode('logo/big/'.$offers['Offer']['creative_1'])); ?>" target="_blank">Image preview</a>
                                            <?php
                                                }
                                             }
                                             ?>                                            
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left" valign="top">&nbsp;Offer Creative 2</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="file" id="creative2" name="data[offer][creative_2]" value="<?php echo ($offers) ? $offers['Offer']['creative_2'] : ""; ?>">
                                            <label style="color: red; font-size: 9px;">Required image format is jpg</label>
                                            <?php
                                             echo "&nbsp;&nbsp;Image Size:&nbsp;" . $form->select('offer.creative2_size', $image_sizes, null, array('id' => 'creative2_size', 'style' => 'width: 150px'));
                                             if ($offers) {
                                                if ($offers['Offer']['creative_2']) {
                                             ?>
                                            <a href="<?php echo $html->url('/admin/preview/'.base64_encode('logo/big/'.$offers['Offer']['creative_2'])); ?>" target="_blank">Image preview</a>
                                            <?php
                                                }
                                             }
                                             ?>                                            
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left" valign="top"><font color="red"><b>*</b></font>&nbsp;Offer Copy</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                        	<!-- Gets replaced with TinyMCE, remember HTML in a textarea should be encoded -->
                                        	<textarea id="elm1" name="data[offer][offer_copy]" rows="15" cols="80" style="width: 100%"><?php echo $offers['Offer']['offer_copy']; ?></textarea>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left"><font color="red"><b>*</b></font>&nbsp;Category</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                 $category = $offers['Offer']['category'];
                                             } else {
                                                $category = null;
                                             }
                                             echo $form->select('category', $ocategory, $category, array('id' => 'ocategory', 'title' => 'Category is a required field<br/>', 'class' => '{required:true}',  'style' => 'width: 250px')); ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Sub Category</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php echo $form->select('sub_category', null, null, array('id' => 'oscategory', 'style' => 'width: 250px'), true); ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left"><font color="red"><b>*</b></font>&nbsp;Search keywords</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][keywords]" id="keywords" title="Keywords is a required field<br/>" value="<?php echo ($offers) ? $offers['Offer']['keywords'] : ""; ?>" class="{required:true}" style="width: 400px">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Country</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                 $country = $offers['Offer']['target_country'];
                                             } else {
                                                 $country = null;
                                             }
                                             echo $form->select('target_country', $countries, $country, array('id' => 'country', 'style' => 'width: 400px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Offer Type</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                              if ($offers) {
                                                 $offer_type_val = $offers['Offer']['offer_type'];
                                             } else {
                                                $offer_type_val = null;
                                             }
                                             echo $form->select('offer_type', $offer_type, $offer_type_val, array('id' => 'offer_type', 'style' => 'width: 100px'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Coupon Code</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][coupon_code]" id="coupon_code" title="" value="<?php echo ($offers) ? $offers['Offer']['coupon_code'] : ""; ?>" class="" style="width: 85px">
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Create Date</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][date_started]" id="create_date" value="<?php echo ($offers) ? date('m-d-Y', strtotime($offers['Offer']['date_started'])) : ""; ?>" style="width: 100px;" /><button type="reset" id="t_trigger_a">...</button>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;End Date</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[offer][date_ended]" id="end_date" value="<?php echo ($offers) ? date('m-d-Y', strtotime($offers['Offer']['date_ended'])) : ""; ?>" style="width: 100px;" /><button type="reset" id="t_trigger_b">...</button>
                                        </td>
            						</tr>
                                    <tr>
                                        <td colspan="2">&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td align="left"><input type="submit" value="Save" style="width: 150px;" class="button"></td>
                                    </tr>
                                </table>
                                <script type="text/javascript">
                                    Calendar.setup({
                                        inputField     :    "create_date",      // id of the input field
                                        ifFormat       :    "%m/%d/%Y",       // format of the input field
                                        showsTime      :    false,            // will display a time selector
                                        button         :    "t_trigger_a",   // trigger for the calendar (button ID)
                                        singleClick    :    false,           // double-click mode
                                        step           :    1                // show all years in drop-down boxes (instead of every other year as default)
                                    });
                                    Calendar.setup({
                                        inputField     :    "end_date",      // id of the input field
                                        ifFormat       :    "%m/%d/%Y",       // format of the input field
                                        showsTime      :    false,            // will display a time selector
                                        button         :    "t_trigger_b",   // trigger for the calendar (button ID)
                                        singleClick    :    false,           // double-click mode
                                        step           :    1                // show all years in drop-down boxes (instead of every other year as default)
                                    });
                                </script>
                            </td>
                            <td width="40%" valign="top">
                                <!-- form fields -->
                                <table class="adminlist" cellpadding=20>
                                    <tr>
                                        <td align="left" width="20%">&nbsp;Form name</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[custom][form_name]" id="form_name" value="<?=($fform)?$fform['Field']['form_name']:""?>" style="width: 245px;" />
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left" width="20%">&nbsp;Offer Post URL</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="data[custom][form_action]" id="form_action" value="<?=($fform)?$fform['Field']['form_action']:""?>" style="width: 245px;" />
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left"><font color="red"><b>*</b></font>&nbsp;Post Method</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($offers) {
                                                 $pmethod = $offers['Offer']['post_method'];
                                             } else {
                                                $pmethod = null;
                                             }
                                             echo $form->select('post_method', $post_method, $pmethod, array('id' => 'post_method', 'title' => 'Post methos is a required field<br/>', 'class' => '{required:true}', 'style' => 'width: 100px'));
                                             ?>
                                        </td>
            						</tr>
                                </table>
                                <br/>
                                <table class="adminlist" cellpadding=20>
                                    <tr>
                                        <th colspan="2">Form Details</th>
                                    </tr>
                                    <tr>
                                        <td align="left" width="27%">&nbsp;Field Type</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($_fieldd) {
                                                $fftype_val = $_fieldd['Field']['field_type'];
                                             } else {
                                                $fftype_val = "";
                                             }
                                             echo $form->select('fftype', $fftype, $fftype_val, array('id' => 'fftype', 'style' => 'width: 250px', 'onchange' => 'javascript: checkfieldtype(this.value); return false;'));
                                             echo $form->select('dateformat', array('1' => 'YYYY-MM-DD HH:MM:SS', '2' => 'MM/DD/YYYY HH:MM:SS'), null, array('id' => 'dateformat', 'style' => 'width: 150px; visibility: hidden'));
                                             ?>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Field label</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="field_label" id="field_label" value="<?=($_fieldd['Field'])?$_fieldd['Field']['field_label']:""?>" style="width: 245px;" />
                                             <div id="labelpos" style="visibility: hidden;">
                                                 <label>Position:</label>
                                                 <select id="labelposval" name="data[custom][labelpos]">
                                                    <option value='left'>L</option>
                                                    <option value='right'>R</option>
                                                 </select>
                                             </div>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Field name</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="field_name" id="field_name" value="<?=($_fieldd['Field'])?$_fieldd['Field']['field_name']:""?>" style="width: 245px;" />
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left" valign="top">&nbsp;Field value</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <input type="text" name="field_value" id="field_value" value="<?=($_fieldd['Field'])?$_fieldd['Field']['field_value']:""?>" style="width: 245px;" />
                                            <ul style="font-size: 11px; color: red; font-style: italic;">
                                                <li>For drop down value, use semi colon to separate the values</li>
                                                <li>Format: 1=>Married;2=>Single</li>
                                            </ul>
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Required</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             if ($_fieldd) {
                                                if ($_fieldd['Field']['required']) {
                                                    $checked = true;
                                                } else {
                                                    $checked = false;
                                                }
                                             } else {
                                                $checked = false;
                                             }
                                             ?>
                                            <input type="checkbox" name="data[custom][required]" id="is_required" checked=<?=$checked?> />
                                        </td>
            						</tr>
                                    <tr>
                                        <td align="left">&nbsp;Validation</td>
                                        <td align="left" style="border: 1px dotted #ccc; background-color: #efefef;">
                                            <?php
                                             $validation_val = isset($_fieldd['Field']['validation']) ? $_fieldd['Field']['validation'] : "";
                                             if ($validation_val == 'maxlength' || $validation_val == 'minlength') {
                                                $visibility = "visible";
                                                $mlength_val = $_fieldd['Field']['validation_param'];
                                             } else {
                                                $visibility = "hidden";
                                                $mlength_val = "";
                                             }
                                             echo $form->select('custom.validation', $validation, $validation_val, array('id' => 'validation', 'style' => 'width: 250px', 'onchange' => 'javascript: checkvalidation(this.value);'));
                                             ?>
                                            <input type="text" id="mlength" name="data[custom][mlength]" value="<?php echo $mlength_val; ?>" style="width:50px; visibility: <?php echo $visibility; ?>;" />
                                        </td>
            						</tr>
            						<tr>
                                        <td align="left">&nbsp;</td>
                                        <td><input type="button" value="Add" style="width: 70px;" class="button" onclick="javascript: checkElementName(document.getElementById('field_name').value);"></td>
                                    </tr>
                                </table>

                                <table class="adminform">
                                    <tr>
                                        <th>Form Fields Preview</th>
                                        <th align="right">
                                            <?php
                                             if ($fields) {
                                                echo $html->link('View Source', '/admin/export_form_fields/'.$offerId, array('target' => '_blank'));
                                             }
                                             ?>
                                        </th>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            <div id="preview"></div>
                                            <?php
                                             if ($fields) {
                                             ?>
                                            <div id="preview_edit">
                                                <form name="<?=$fform['Field']['form_name']?>" action="<?=$fform['Field']['form_action']?>" method="<?=($offers['Offer']['post_method']==0)?'post':'get'?>">
                                                <table cellpadding="20px" class="adminlist">
                                            <?php
                                                 foreach ($fields as $v) {
                                                     if ($v['Field']['field_type']) {
                                                        // add jquery validation
                                                        if ($v['Field']['validation']) {
                                                            $pos = stripos($v['Field']['validation'], "length");
                                                            if ($pos === false) {
                                                                $class = $v['Field']['validation'].":true";
                                                            } else {
                                                                $class = $v['Field']['validation'].":".$v['Field']['validation_param'];
                                                            }
                                                        }

                                                        // check if field is required, if yes, add it to validation
                                                        if ($v['Field']['required'] == 1) {
                                                            if (isset($class)) {
                                                                $class = "{required:true,".$class."}";
                                                            } else {
                                                                $class = "{required:true}";
                                                            }

                                                        }
                                             ?>
                                                    <tr>
                                                        <td width="20%">
															<?php
															 if ($v['Field']['field_type'] == 'hidden') {
																echo "<font color='red'>{hidden field}</font>";
															 } else {
																if ($v['Field']['extras']) {

																    if ($v['Field']['extras'] == 'right') {
															 ?>
															<input type="checkbox" id="<?php echo $v['Field']['field_name']?>" name="" />
															<?php
																    } else {
																    	echo $v['Field']['field_label'];
																    }

																} else {
																    echo $v['Field']['field_label'];
                                                                }
															 }
															 ?>
														</td>
                                                        <td width="%">
                                                        <?php
                                                         switch($v['Field']['field_type']){
                                                            case "select":
                                                                $options = explode(";", html_entity_decode($v['Field']['field_value']));
                                                         ?>
                                                            <select id="<?php echo $v['Field']['field_name']?>" name="<?php echo $v['Field']['field_name']?>" class="<?php echo $class; ?>">
                                                        <?php
                                                         foreach ($options as $opt) {
                                                                if (stripos($opt, '=>') === false) {
                                                         ?>
                                                                <option value="<?php echo $opt?>"><?php echo $opt?></option>
                                                        <?php
                                                                } else {
                                                                    $key = explode("=>", $opt);
                                                         ?>
                                                                <option value="<?php echo $key[0]?>"><?php echo $key[1]?></option>
                                                        <?php
                                                                }

                                                         }
                                                         ?>
                                                            </select>
                                                        <?php
                                                                break;

                                                            case "createdate":
                                                                if ($v['Field']['extras'] == 1) {
                                                         ?>
                                                        <input type="text" value="<?php echo date('Y-m-d H:i:s'); ?>" style='border: 1px dashed #FF1F1F; background-color: #efefef;' readonly>
                                                        <?php
                                                                } else {
                                                         ?>
                                                        <input type="text" value="<?php echo date('m-d-Y H:i:s'); ?>" style='border: 1px dashed #FF1F1F; background-color: #efefef;' readonly>
                                                        <?php
                                                                }
                                                         ?>                                                         
                                                             <input type="hidden" id="<?php echo $v['Field']['field_name']?>" name="<?php echo $v['Field']['field_name']?>" />
                                                        <?php
                                                                break;

															case "ipaddress":
													     ?>
                                                             <input type="text" value="<?php echo $_SERVER['REMOTE_ADDR'] ?>" style='border: 1px dashed #FF1F1F; background-color: #efefef;' readonly>
                                                             <input type="hidden" id="<?php echo $v['Field']['field_name']?>" name="<?php echo $v['Field']['field_name']?>" />
                                                        <?php
																break;

                                                            case "checkbox":
                                                                if ($v['Field']['extras'] == 'left') {
                                                                    echo $v['Field']['field_label'];
                                                         ?>
                                                        <input type="checkbox" id="<?php echo $v['Field']['field_name']?>" name="<?php echo $v['Field']['field_name']?>" value="<?php echo $v['Field']['field_value']?>">
                                                        <?php
                                                                } else {
                                                         ?>
                                                        <input type="checkbox" id="<?php echo $v['Field']['field_name']?>" name="<?php echo $v['Field']['field_name']?>" value="<?php echo $v['Field']['field_value']?>">
                                                        <?php
                                                                    echo $v['Field']['field_label'];
                                                                } // end if
                                                                break;

                                                            default:
																if ($v['Field']['field_type'] == 'hidden') {
														?>
														    <input type="text" style="border: 1px solid #ccc;" value="<?php echo ($v['Field']['field_value'])?$v['Field']['field_value']:"";?>">
													   <?php
																} else {
                                                         ?>
                                                            <input type="<?php echo $v['Field']['field_type']?>" id="<?php echo $v['Field']['field_name']?>" name="<?php echo $v['Field']['field_name']?>" value="<?php echo ($v['Field']['field_value'])?$v['Field']['field_value']:"";?>" class="<?php echo $class; ?>">
                                                        <?php
																}

                                                                break;
                                                         } // end switch
                                                         ?>
                                                        </td>
                                                        <td width="10%">
                                                            <a href="<?php echo $html->url('/admin/add_offers/'.$offerId.'/f/delete/'.$v['Field']['id']); ?>"><?php echo $html->image('admin/publish_x.png', array('border' => 0, 'alt' => 'Delete')); ?></a>
                                                        </td>
                                                    </tr>
                                            <?php
                                                     }
                                                 }
                                             ?>
                                                </table>

                                                <input type="hidden" id="edit_field_id" name="data[field][edit_field_id]" value="<?=$edit_field_id?>">
                                                </form>
                                            </div>
                                            <?php
                                             }
                                             ?>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>

                    <input type="hidden" name="data[offer][manage]" id="offer" />
                    <input type="hidden" id="flag" name="data[window][flag]">
                    </form>
                </td>
            </tr>
        </table>

    </div>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $("#ocategory").change(function() {
            var cat = $("#ocategory").val();
            var subcat = eval('<?php echo $oscategory; ?>');
            var option = '';
            option += "<option value=''>...</option>";
            $.each(subcat, function(key, value) {
                if(cat == value.parent_id) {
                    option +=  "<option value='"+ value.id+"'>"+ value.offer_category+"</option>";
                }
            });
            $("#oscategory").html(option);
        });

        $("#creative1").blur(function() {
            if ($("#creative1").val().indexOf("jpg") < 0) {
                alert('Image format should be in jpg');
            }
        });

        $("#creative2").blur(function() {
            if ($("#creative2").val().indexOf("jpg") < 0) {
                alert('Image format should be in jpg');
            }
        });
    });


    function checkElementName(val)
    {
        var cn = document.getElementById("preview").childNodes;
        var fn = document.getElementById("field_name").value;
        var fv = document.getElementById("field_value").value;
        var proceed = true;

        //if (fn == "" || fv == "") {
        //    alert("Incomplete details");
        //    return false;
        //}

        for (var i = 0; i < cn.length; i++) {
            if ( cn[i].nodeType==1 ) {
                if (cn[i].value == val) {
                    proceed = false;
                    alert("field name already exists");
                    return false;
                } else {
                    proceed = true;
                }
            } else {
                proceed = true;
            }
        }

        if (proceed) {
            createCustomElement();
        }
    }

    function deleteItem(objId)
    {
        document.getElementById(objId + '-delete').value = 1;
        document.getElementById(objId).style.visibility = "hidden";
    }

    function createCustomElement()
    {
        var field_type = document.getElementById('fftype');
        var field_label = document.getElementById('field_label').value;
        var field_name = document.getElementById('field_name').value;
        var field_value = document.getElementById('field_value').value;
        var field_validation = document.getElementById('validation').value;
        var required = document.getElementById('is_required').checked;

        if (field_type.value) {
            var element;
            switch (field_type.value) {
                case "text":
                    element = "<div id='"+field_type.value+"'>";
                    element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                    element += "<label>"+field_label+"</label>&nbsp;";
                    element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' name='data[custom][field_validation-"+field_name+"]' value='"+field_validation+"'>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";
                    element += "</div>";
                    break;

                case "hidden":
                    element = "<div id='"+field_type.value+"'>";
                    element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                    element += "<label>"+field_label+"</label>&nbsp;";
                    element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"]' value='"+field_value+"' style='border: 1px dashed #FF1F1F; background-color: #efefef;'>";
                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";
                    break;

                case "checkbox":
                    if (document.getElementById('labelposval').value == 'left') {
                        element = "<div id='"+field_type.value+"'>";
                        element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                        element += "<label>"+field_label+"</label>&nbsp;";
                        element += "<input type='checkbox' id='"+field_name+"' name='data[custom]["+field_name+"]' value='"+field_value+"'>";
                    } else {
                        element = "<div id='"+field_type.value+"'>";
                        element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                        element += "<input type='checkbox' id='"+field_name+"' name='data[custom]["+field_name+"]' value='"+field_value+"'>";
                        element += "<label>"+field_label+"</label>&nbsp;";
                    }

                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' name='data[custom][field_labelpos-"+field_name+"]' value='data[custom][labelpos]'>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";

                    document.getElementById('labelpos').style.visibility = "hidden";
                    break;

                case "phone":
                    element = "<div id='"+field_type.value+"'>";
                    element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                    element += "<label>"+field_label+"</label>&nbsp;";
                	element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"][]' value='"+field_value+"' size='5'>-";
                	element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"][]' value='"+field_value+"' size='10'>-";
                	element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"][]' value='"+field_value+"' size='10'>";
                    element += "<input type='hidden' name='data[custom][field_type-"+field_name+"][]' value='"+field_type.value+"'>";
                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' name='data[custom][field_value-"+field_name+"][]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";
                    break;

                case "birthday":
					var month = "<select name='data[custom]["+field_name+"][]'>";
					month += "<option value='1'>January</option>";
					month += "<option value='2'>February</option>";
					month += "<option value='3'>March</option>";
					month += "<option value='4'>April</option>";
					month += "<option value='5'>May</option>";
					month += "<option value='6'>June</option>";
					month += "<option value='7'>July</option>";
					month += "<option value='8'>August</option>";
					month += "<option value='9'>September</option>";
					month += "<option value='10'>October</option>";
					month += "<option value='11'>November</option>";
					month += "<option value='12'>December</option>";
					month += "</select>";

                    element = "<div id='"+field_type.value+"'>";
                    element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                    element += "<label>"+field_label+"</label>&nbsp;";
                	element += month + "&nbsp;";
                	element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"][]' value='"+field_value+"' size='3' maxlength='2'>,&nbsp;";
                	element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"][]' value='"+field_value+"' size='5' maxlength='4'>";
                    element += "<input type='hidden' name='data[custom][field_type-"+field_name+"][]' value='"+field_type.value+"'>";
                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' name='data[custom][field_value-"+field_name+"][]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";
                	break;

                case "createdate":
                    element = "<div id='"+field_type.value+"'>";
                    element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                    element += "<label>"+field_label+"</label>&nbsp;";
                	
                    if (document.getElementById('dateformat').value == 1) {
                        element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"]' value='<?php echo date('Y-m-d H:i:s'); ?>' style='border: 1px dashed #FF1F1F; background-color: #efefef;'>,&nbsp;";
                        element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                    } else {
                        element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"]' value='<?php echo date('m-d-Y H:i:s'); ?>' style='border: 1px dashed #FF1F1F; background-color: #efefef;'>,&nbsp;";
                        element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                    }

                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' id='" + field_name + "' name='data[custom][field_value-"+field_name+"]' value=''>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' id='dateformat_val' name='data[custom][field_labelpos-"+field_name+"]' value=''>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";
                    element += "<script type='text/javascript'>";
                    element += "document.getElementById('dateformat_val').value = document.getElementById('dateformat).value;\r\n";
                    element += "getsysdatetime('"+field_name+"', document.getElementById('dateformat').value);";
                    element += "<\/script>";
                    break;

                case "ipaddress":
                    element = "<div id='"+field_type.value+"'>";
                    element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                    element += "<label>"+field_label+"</label>&nbsp;";
                	element += "<input type='text' id='"+field_name+"' name='data[custom]["+field_name+"]' value='<?php echo $_SERVER['REMOTE_ADDR']; ?>' style='border: 1px dashed #FF1F1F; background-color: #efefef;'>,&nbsp;";
                    element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' id='" + field_name + "' name='data[custom][field_value-"+field_name+"]' value=''>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' id='ip_val' name='data[custom][field_labelpos-"+field_name+"]' value=''>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";
                    //element += "<script type='text/javascript'>";
                    //element += "yhostip();";
                    //element += "<\/script>";
                    break;

                case "salutation":
                    var salutation = "<select name='data[custom]["+field_name+"][]'>";
                    salutation += "<option value='Mr.'>Mr.</option>";
                    salutation += "<option value='Mrs.'>Mrs.</option>";
                    salutation += "<option value='Ms.'>Ms.</option>";
                    salutation += "<option value='Miss'>Miss</option>";
                    salutation += "<option value='Dr.'>Dr.</option>";
                    salutation += "</select>";

                    element = "<div id='"+field_type.value+"'>";
                    element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                    element += "<label>"+field_label+"</label>&nbsp;";
                    element += salutation;
                    element += "<input type='hidden' name='data[custom][field_type-"+field_name+"][]' value='"+field_type.value+"'>";
                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";
                	break;

                case "select":
                    var _fv = field_value.replace(" ", "");
                    var fv = field_value.split(';');
                    var option;

                    for (i=0; i < fv.length; i++) {
                        if (fv[i]) {

                            if (fv[i].indexOf('=>') == -1) {
                                option += "<option value='"+fv[i]+"'>"+fv[i]+"</option>";
                            } else {
                                tmp = fv[i].split('=>');
                                option += "<option value='"+tmp[0]+"'>"+tmp[1]+"</option>";
                            }


                        }
                    }

                    element = "<div id='"+field_type.value+"'>";
                    element += "[ <a href=\"javascript: deleteItem('"+field_type.value+"');\">Delete</a> ]&nbsp;&nbsp;";
                    element += "<label>"+field_label+"</label>&nbsp;";
                    element += "<select name='"+field_name+"'>";
                    element += option;
                    element += "</select>";
                    element += "<input type='hidden' name='data[custom]["+field_name+"]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_label-"+field_name+"]' value='"+field_label+"'>";
                    element += "<input type='hidden' name='data[custom][field_type-"+field_name+"]' value='"+field_type.value+"'>";
                    element += "<input type='hidden' name='data[custom][field_name-"+field_name+"]' value='"+field_name+"'>";
                    element += "<input type='hidden' name='data[custom][field_value-"+field_name+"]' value='"+field_value+"'>";
                    element += "<input type='hidden' name='data[custom][field_required-"+field_name+"]' value='"+required+"'>";
                    element += "<input type='hidden' id='"+field_type.value+"-delete' name='data[custom][field_delete-"+field_name+"]' value='0'>";
                    break;
            }
        }

        var current = document.getElementById('preview').innerHTML;
        var preview = document.getElementById('preview');

        preview.innerHTML = current + '<br/>' + element;
        if (document.getElementById('edit_field_id')) {
            document.getElementById('edit_field_id').value = "";
        }


        clearFields();

    }

    function clearFields()
    {
        document.getElementById('fftype').selectedIndex = 0;
        document.getElementById('field_label').value = '';
        document.getElementById('field_name').value = '';
        document.getElementById('field_value').value = '';
        document.getElementById('is_required').checked = false;
        document.getElementById('validation').selectedIndex = 0;
    }

    function submitthis()
    {
        var flag = document.getElementById('flag').value;
        switch (flag) {
            case "offer":
                document.getElementById('offer').value = 'manage';
                document.businessForm.submit();
                break;

            case "disable":
                var msg = "Are you sure you want to cancel?";
                if (confirm(msg)) {
                    document.businessForm.submit();
                }
                break;

            /*case "delete":
                var msg = "Are you sure you want to delete the selected advertiser?";
                if (confirm(msg)) {
                    document.businessForm.submit();
                }
                break;*/

            default:
                document.businessForm.submit();
                break;
        }
    }

    function checkmsg()
    {
        var msg = '<?php echo $alert; ?>';
        if (msg) {
            alert(msg);
        }
    }

    function checkvalidation(validate)
    {
        switch (validate) {
            case "maxlength" && "minlength":
                document.getElementById('mlength').style.visibility = "visible";
                document.getElementById('mlength').focus();
                break;

            default:
                document.getElementById('mlength').style.visibility = "hidden";
                break;
        }
    }

    function checkfieldtype(fieldtype)
    {
        switch (fieldtype) {
            case "checkbox":
                document.getElementById('labelpos').style.visibility = "visible";
                document.getElementById('dateformat').style.visibility = "hidden";
                break;

            case "createdate":
            	document.getElementById('dateformat').style.visibility = "visible";
            	document.getElementById('labelpos').style.visibility = "hidden";
                break;

            default:
            	document.getElementById('dateformat').style.visibility = "hidden";
                document.getElementById('labelpos').style.visibility = "hidden";
                break;
        }
    }

    checkmsg();
</script>